// 引入axios获取数据插件
import axios from "axios";
// 引入vant
import { createApp } from "vue";
import { Toast } from "vant";
const app = createApp();
app.use(Toast);
//定义一个实例对象
const instance = axios.create({
  //数据请求地址
  baseURL: "http://121.40.124.132:3000/api",
  //最长响应时间
  timeout: 10000,
});

const http = {
  get(url, params) {
    return new Promise((resolve) => {
      instance
        .get(url, {
          params: params,
        })
        .then((res) => {
          // 处理成功情况
          if (res.data.status === 0) {
            resolve(res.data.result);
          }
        })
        .catch(() => {
          // 处理错误情况
          Toast("错误");
        });
    });
  },
  post(url, params) {
    return new Promise((resolve) => {
      instance
        .post(url, params)
        .then((res) => {
          // 处理成功情况
          resolve(res);
        })
        .catch((error) => {
          // 处理错误情况
          Toast(error);
        });
    });
  },
};

export default http;
